﻿using System;
using FlashCards.SL.FlashCardCreator;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using FlashCards.SL.FlashCardCreator.ViewModels.Commands;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.Text;
using FlashCards.Dto;
using FlashCards.SL.FlashCardCreator.ViewModels.Utils;
using System.Linq;
using FlashCards.SL.FlashCardCreator.Utils;

namespace FlashCards.SL.FlashCardCreator.ViewModels
{
    public class FlashcardPartViewModel : BaseViewModel
    {
        private FlashcardPart _flashcardPart;
        private IContentViewModel _contentViewModel;

        public FlashcardPartViewModel(FlashcardPart part)
        {
            _flashcardPart = part;

            InitializeCommands();
            PropertyChanged += HandlePropertyChanged;

            var types = ((FlashCardsSilverlightApp)System.Windows.Application.Current).Resources["FlashcardTypeParts"] as List<FlashcardPartType>;
            if (types != null && types.Count > 0)
            {
                if (part.Type == null)
                    SelectedPartType = types[0];
                else
                    SelectedPartType = types.FirstOrDefault(x => x.Name == part.Type.Name);
            }

        }

        public IContentViewModel ContentViewModel
        {
            get { return _contentViewModel; }
            set { _contentViewModel = value; OnPropertyChanged("ContentViewModel"); }
        }

        public FlashcardPartType SelectedPartType
        {
            get { return _flashcardPart.Type; }
            set { _flashcardPart.Type = value; OnPropertyChanged("SelectedPartType"); }
        }

        private BitmapImage _image;
        public BitmapImage Image
        {
            get { return _image; }
            set { _image = value; OnPropertyChanged("Image"); }
        }

        #region PROPERTY CHANGED HANDLERS

        void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedPartType")
            {
                SelectedPartTypeChanged();
            }
        }

        void SelectedPartTypeChanged()
        {
            if (SelectedPartType.Name == "Text")
            {
                ContentViewModel = new TextContentViewModel(_flashcardPart);
            }
        }

        #endregion

        #region COMMANDS

        public ICommand SelectImageFileCommand { get; private set; }
        public ICommand ReplaceAccentCharacterCommand { get; private set; }

        void InitializeCommands()
        {
            SelectImageFileCommand = new Command(SelectImageFile);
            ReplaceAccentCharacterCommand = new CommandWithParam(ReplaceAccentCharacter);
        }

        void SelectImageFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.ShowDialog();

            if (dialog.File != null)
            {
                var stream = dialog.File.OpenRead();
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);

                var memStream = new MemoryStream(buffer);

                var loadedBitmap = new BitmapImage();
                loadedBitmap.SetSource(memStream);

                Image = loadedBitmap;
            }
        }

        void ReplaceAccentCharacter(object o)
        {
            var accentParams = o as AccentParameters;
            if (ContentViewModel is TextContentViewModel)
            {
                var contentModel = ContentViewModel as TextContentViewModel;
                contentModel.Text = contentModel.Text.Remove(accentParams.Index, 1);
                contentModel.Text = contentModel.Text.Insert(accentParams.Index, accentParams.NewCharacter.ToString());
            }
        }

        #endregion
    }
}
